import React,{Component} from 'react'
import {PropTypes} from 'prop-types'

let connect=(mapStateToProps,mapDispatchToProps)=>(WrapComponent)=>{
    class Proxy extends Component{
        constructor(){
            super()
            this.state={};
        }
        componentWillMount() {
            //订阅
            this.context.store.subscribe(() => {
                this.unsubscribe=this.setState(mapStateToProps(this.context.store.getState()));
            })
        }
        componentWillUnmount() {
            //取消订阅
            this.unsubscribe();
        }
        render(){
            return <WrapComponent {...this.state} {...mapDispatchToProps(this.context.store.dispatch)}/>
        }
    }
    Proxy.contextTypes={
        store:PropTypes.object
    }
    return Proxy
};
export default connect;